<template>
  <div>
    <div class="table-tool">
      <el-button class="el-button--blue" type="primary" icon="el-icon-upload" @click="addDialogShow">添加</el-button>
      <el-button class="el-button--blue" type="primary" icon="el-icon-delete" @click="deleteLabel">删除</el-button>
    </div>
    <el-table ref="table_container" header-align="center" :data="data" stripe :height="tableHeight" v-loading="loading" @selection-change="handleSelectionChange" header-cell-class-name="table-header-cell" cell-class-name="table-cell">
      <el-table-column type="selection" fixed/>
      <el-table-column prop="index" label="编号"/>
      <el-table-column label="标签名称">
        <template slot-scope="scope">
          <el-tag :type="scope.row.color">{{scope.row.value}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="230px" fixed="right">
        <template slot-scope="scope">
          <el-button class="text-button" type="text" icon="el-icon-edit blue" @click="changeInfoDialogShow(scope.$index)">修改信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="changeDialog.title" :visible.sync="changeDialog.visible" width="1200px">
      <label-change v-model="changeDialog"/>
    </el-dialog>
    <el-dialog :title="addDialog.title" :visible.sync="addDialog.visible" width="500px">
      <label-add v-model="addDialog"/>
    </el-dialog>
  </div>
</template>

<script>
import labelAdd from '../dialog/label-add'
import labelChange from '../dialog/label-change'
import { GetLabelTable } from '../../../api/table'
import { SubmitLabelDelete } from '../../../api/form'
import { mapGetters } from 'vuex'
export default {
  name: 'label-management-table',
  components: {
    labelAdd,
    labelChange
  },
  data () {
    return {
      loading: false,
      tableHeight: '0px',
      pagination: {
        pageSize: 10,
        currentPage: 1
      },
      total: 0,
      multipleSelection: [],
      data: [],
      addDialog: {
        title: '',
        visible: false
      },
      changeDialog: {
        title: '',
        visible: false,
        data: {
          id: '',
          value: '',
          color: ''
        }
      }
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.pagination.pageSize = val
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
    },
    changeInfoDialogShow (index) {
      this.changeDialog.data.id = this.data[index].id
      this.changeDialog.data.value = this.data[index].value
      this.changeDialog.data.color = this.data[index].color
      this.changeDialog.visible = true
    },
    addDialogShow () {
      this.addDialog.visible = true
    },
    flashTable () {
      this.loading = true
      GetLabelTable(this.pagination.pageSize, this.pagination.currentPage).then((res) => {
        this.total = res.data.total
        this.data = res.data.data
        this.loading = false
      })
    },
    deleteLabel () {
      SubmitLabelDelete(this.multipleSelection).then(res => {
        this.multipleSelection = []
        this.$store.dispatch('SetFlash')
      })
    },
    reInitTableHeight () {
      if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop < 0) {
        setTimeout(this.reInitTableHeight, 10)
      } else if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop <= 500) {
        this.tableHeight = '500px'
      } else {
        this.tableHeight = document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop + 'px'
      }
    }
  },
  mounted () {
    setTimeout(this.reInitTableHeight, 100)
  },
  computed: {
    ...mapGetters([
      'flash',
      'height'
    ])
  },
  watch: {
    height: function (val) {
      this.$nextTick(this.$nextTick(this.reInitTableHeight))
    },
    'pagination.currentPage': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    'pagination.pageSize': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    flash: function () {
      this.flashTable()
    }
  }
}
</script>

<style scoped>
  @import "../../../styles/common.css";
  .table-tool {
    width: 100%;
    padding: 15px 10px;
  }
</style>
